package tgc.edu.tms.web.controller;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;

import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;

import org.abyss.data.specifications.Rule;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSON;

import tgc.edu.tms.custom.AjaxResult;
import tgc.edu.tms.custom.CommonController;
import tgc.edu.tms.custom.GenericsUtils;
import tgc.edu.tms.custom.TablePageable;
import tgc.edu.tms.custom.TimeUtils;
import tgc.edu.tms.entity.ClassSchedule;
import tgc.edu.tms.entity.Curriculum;
import tgc.edu.tms.entity.FlowTable;
import tgc.edu.tms.entity.SysDept;
import tgc.edu.tms.entity.TbClass;
import tgc.edu.tms.entity.Teacher;
import tgc.edu.tms.entity.Teaching;
import tgc.edu.tms.entity.Teachingplan;
import tgc.edu.tms.entity.TtbRecord;
import tgc.edu.tms.security.UserUtils;
import tgc.edu.tms.service.ClassScheduleService;
import tgc.edu.tms.service.CurriculumService;
import tgc.edu.tms.service.FlowTableService;
import tgc.edu.tms.service.SysDeptService;
import tgc.edu.tms.service.TeacherService;
import tgc.edu.tms.service.TeachingService;
import tgc.edu.tms.service.TeachingplanService;
import tgc.edu.tms.service.TtbRecordService;
import tgc.edu.tms.web.form.FlowTableForm;
import tgc.edu.tms.web.form.TeachingForm;

@Controller
@RequestMapping(value = "/teaching")
public class TeachingController extends CommonController<FlowTable, Integer,FlowTableForm> {
	// 部门
	@Autowired
	private SysDeptService sysDeptService;

	// 教师
	@Autowired
	private TeacherService teacherService;

	// 课程
	@Autowired
	private ClassScheduleService classScheduleServie;

	@Autowired
	private UserUtils userutils;

	@Autowired
	private TeachingplanService teachingplanService;

	// 授课日志
	@Autowired
	private TeachingService teachingService;

	// 停课
	@Autowired
	private TtbRecordService ttbRecordService;

	@Autowired
	private CurriculumService curriculumService;
	
	@Autowired 
	private FlowTableService flowTableService;

	@Override
	public void manage(ModelMap map) {	
		List<String> teacherlist = flowTableService.findAllTeacher();	//教师
		List<String> yearlist = flowTableService.findAllYear();			//学年
		List<String> timelist = new ArrayList<>();
		String ntime = "";
		String ytime = "";
		for (String time : yearlist) {									//截取年份
			try {
				String[] timesp = time.split("-");
				ntime = timesp[0];
			} catch (Exception e) {
				
			}
	            if(!ntime.equals(ytime)) {
	            	ytime = ntime;
	            	timelist.add(ntime);
	            }
		}
		map.put("teacherlist", teacherlist);
		map.put("yearlist",timelist);
		super.manage(map);
	}

	/***
	 * 教师发送变化时加载课程
	 * @param value	教师
	 * @return
	 */
	@RequestMapping(value = "/teacherChange")
	@ResponseBody
	public Object teacherChange(String teacher) {
		List<String> list = flowTableService.findAllCourse(teacher);
		return list;
	}
	
	/***
	 * 学年发生变化时加载学期
	 * @param year 学年
	 * @return
	 */
	@RequestMapping(value="/yearChange")
	@ResponseBody
	public Object yearChange(String year) {
		List<String> list = flowTableService.findAllTerm(year);
		return list;
	}
	
	/*** 
	 * 授课日志录入
	 * @return
	 */
	@RequestMapping(value="/addteaching")
	public String addteaching() {
		return "teaching/addteaching";
	}
	
	/***
	 * 时间发生变化后带的处理
	 * @param time
	 * @return
	 */
	@RequestMapping(value="/time")
	@ResponseBody
	public Object time(String time) {
		String name = userutils.getUser().getName();	//教师用户名
		List<FlowTable> flowTable = flowTableService.findByTeacherAndTimeAndSknr(name,time,"");
		List<FlowTable> flowTable1 = flowTableService.findByTeacherAndTimeAndSknr(name,time,null);
		flowTable.addAll(flowTable1);
		return flowTable;
	}
	
	/***
	 * 节次发生变化后的处理
	 * @param kc
	 * @return
	 */
	@RequestMapping(value="/content")
	@ResponseBody
	public Object kc(Integer kc) {
		String name = userutils.getUser().getName(); 			//教师姓名
		FlowTable flowTable = flowTableService.findById(kc);	//用课程id找到这条记录
		String kcname = flowTable.getCourse();					//获取到课程名称
		Specification<Teachingplan> spec = new Specification<Teachingplan>() {
			private static final long serialVersionUID = 1L;

			@Override
			public Predicate toPredicate(Root<Teachingplan> root, CriteriaQuery<?> query, CriteriaBuilder cb) {
				HashSet<Predicate> rules = new HashSet<>();
				if(StringUtils.hasText(kcname)) {
					rules.add(cb.equal(root.get("curriculum"), kcname)); 
				}
				if(StringUtils.hasText(name)) {
					rules.add(cb.equal(root.get("teacher").get("name"), name));
				}
				return cb.and(rules.toArray(new Predicate[rules.size()]));
			}
		};
		List<Teachingplan> teachingplan = teachingplanService.findAll(spec);
		if(teachingplan.size()==0 && kc!=null) {
			Teachingplan teachingplan1 = new Teachingplan();
			teachingplan1.setContent("自习");
			teachingplan.add(teachingplan1);
		}
		return teachingplan;
	}
	
	/***
	 * 填值
	 * @param id 记录
	 * @return
	 */
	@RequestMapping(value="/data")
	@ResponseBody
	public Object data(Integer id) {
		FlowTable flowTable = flowTableService.findById(id);
		return flowTable;
	}
	
	/***
	 * 授课日志提交
	 */
	@Override
	public Object save(FlowTableForm form) {
		try {
			Integer id = form.getId();
			FlowTable flowTable = flowTableService.findById(id);
			Integer sdrs = form.getSdrs(); 					//实到人数等于应到人数
			Integer qqrs = flowTable.getQqrs();				//缺勤人数
		//	flowTable.setSdrs(sdrs);						//人数
			flowTable.setRemarks(form.getRemarks());		//备注
		//	flowTable.setQqrs(qqrs);						//缺勤人数
			flowTable.setSknr(form.getSknr()); 				//上课内容
			flowTableService.save(flowTable);
			return new AjaxResult("数据保存成功");
		} catch (Exception e) {
			return new AjaxResult(false,"数据保存失败");
		}
	}
	
	/***
	 * 授课日志显示
	 */
	@Override
	public Object page(TablePageable pageParam, FlowTableForm form) {
		PageRequest pageable = pageParam.bulidPageRequest();
		String teacher = form.getTeacher();			//教师
		String course = form.getCourse();			//课程
		String year = form.getYear();				//学年
		String term = form.getTerm(); 				//学期
		Specification<FlowTable> spec = new Specification<FlowTable>() {
			private static final long serialVersionUID = 1L;

			@Override
			public Predicate toPredicate(Root<FlowTable> root, CriteriaQuery<?> query, CriteriaBuilder cb) {
				HashSet<Predicate> rules = new HashSet<>();
				
				if(StringUtils.hasText(form.getYear())) {
					rules.add(cb.like(root.get("time"), "%"+year+"%"));
				}
				
				if(StringUtils.hasText(form.getTerm())) {
					rules.add(cb.equal(root.get("term"), term));
				}
				
				if(StringUtils.hasText(form.getCourse())) {
					rules.add(cb.like(root.get("course"), "%"+course+"%"));
				}
				if(StringUtils.hasText(form.getTeacher())) {
					rules.add(cb.equal(root.get("teacher"), teacher));
				}
				rules.add(cb.notEqual(root.get("sknr"),""));
				return cb.and(rules.toArray(new Predicate[rules.size()]));
			}
		};
		Page<FlowTable> page=flowTableService.findAll(spec, pageable);
		return AjaxResult.bulidPageResult(page);
	}
}
